package top.jacktgq.hsp.netty.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @Author CandyWall
 * @Date 2020/11/12--22:25
 * @Description netty服务器业务处理器：测试心跳机制
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    /**
     * @param ctx ：上下文
     * @param evt ：事件
     * @throws Exception
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            String eventType = null;
            switch (idleStateEvent.state()) {
                case READER_IDLE:
                    eventType = "读空闲";
                    break;
                case WRITER_IDLE:
                    eventType = "写空闲";
                    break;
                case ALL_IDLE:
                    eventType = "读写空闲";
                    break;
            }
            System.out.println(ctx.channel().remoteAddress().toString().substring(1) + " 发生了 " + eventType);
        }
    }
}
